package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.UserAndSoul;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

/**
 * @Description:
 * @Create by: wanglei
 * @Date: 2021/11/7 12:59
 */
//用户和试卷之间的联系
@DubboService
public class UserAndSoulApiImpl implements UserAndSoulApi{
    @Autowired
    private MongoTemplate mongoTemplate;

    //保存
    @Override
    public void save(UserAndSoul userAndSoul) {
        mongoTemplate.save(userAndSoul);
    }

    //根据用户id和soulId查询
    @Override
    public UserAndSoul findByUserIdAndSoulId(Long userId, String soulId) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("soulId").is(soulId));
        UserAndSoul userAndSoul = mongoTemplate.findOne(query, UserAndSoul.class);
        return userAndSoul;
    }

    //根据用户id和soulId更新，isLock为0（是否锁住（0解锁，1锁住））
    @Override
    public void updateByUserIdAndSoulId(Long userId, String soulId) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("soulId").is(soulId));
        Update update = new Update();
        update.set("isLock",0);
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true) ;//获取更新后的最新数据
        Movement modify = mongoTemplate.findAndModify(query, update, options, Movement.class);
    }

    //根据reporetId查询
    @Override
    public List<UserAndSoul> findByReportId(Integer reporetId) {
        Query query = Query.query(Criteria.where("reportId").is(reporetId));
        List<UserAndSoul> userAndSouls = mongoTemplate.find(query, UserAndSoul.class);
        return userAndSouls;
    }
}
